package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.ArrayProblem;
import cn.pugle.oj.catalog.BitProblem;
import cn.pugle.oj.catalog.MathProblem;

/**
 * https://leetcode.com/problems/powx-n/
 * https://www.jianshu.com/p/ec0b97676c3e
 * <p>
 * 艹艹, 根本不是超时了, 而是n过大, 32位int首位是标识正负的
 *
 * @author tzp
 * @since 2020/11/3
 */
public class LC50_1 implements ArrayProblem, MathProblem, BitProblem {

    public double myPow(double x, int n) {
        double result = 1;
        double by2 = x;
        boolean neg = n < 0;
        n = Math.abs(n);
        int tmp = 1;
        while (tmp <= n) {
            if ((n & tmp) == tmp)
                result *= by2;
            tmp = tmp << 1;
            by2 = by2 * by2;
        }
        return neg ? 1 / result : result;
    }

    public static void main(String[] args) {
//        System.out.println(new LC50().myPow(2, 3));
//        System.out.println(new LC50().myPow(2, 10));
//        System.out.println(new LC50().myPow(2, -2));
        System.out.println(new LC50_1().myPow(0.00001, 2147483647));//艹, 还是超时了
    }
}
